#!/bin/bash
# autobackux - Backup your files
# Jon Peeters - June 2011

# autobackux is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

labels="EditBackup"
 
display_backups() {
	load_backups
	if [[ ${#backups[@]} -eq 0 ]]; then
		return
	fi
	results="$_display_backups_title_\n______________________________\n"
	size=${#backups[@]}	
	i=0	
	while [[ $i -lt $size ]]; do	
		parseRecord "${backups[$i]}"
		backup_date=""
		if [[ "${values[$DTE]}" != "0" ]]; then
			tmp=$(date --date="1970-01-01 ${values[$DTE]} sec GMT" +%d/%m/%Y)
			backup_date="($tmp)"
		fi
		b="${values[$NME]} : ${values[$SRC]} => ${values[$DST]} $backup_date"
		results="$results\n$b"
		((i++))
	done
	zenity --info --no-wrap --text="$font_zenity$results</span>" &
}

add_backup() {
	source="-"
	while [[ -n "$source" ]]; do	
		source=$(zenity --file-selection --directory --title="$_add_backup_title_")
		if [[ -n "$source" ]]; then 
			name=$(zenity --entry --title="" --text="$_add_backup_name_")
			if [[ -z "$name" ]]; then
				echo -e "* No backup name => cancel"
				return
			fi			
			hget "$key_default_dest"
			if [[ -n "$values" ]]; then
				dest="$values/"$(basename "$source")
				if [[ -d $dest ]]; then
					display_error "$_add_backup_error_existing_ ($dest)"
					continue
				else
					mkdir "$dest"
					if [[ $? -ne 0 ]]; then
						display_error "$_add_backup_error_cannot_create_ ($dest)"
						continue
					fi
				fi
				echo "0|$name|$source|$dest" >> $dirs_file
			else
				display_error "$_no_default_dir_defined_"
			fi
		else
			echo "* No backup path => cancelled or finished"
			break
		fi
	done
	load_backups force
}

move_backup() {
	load_backups
	choice="-"
	while [[ -n "$choice" ]]; do
		parseAllRecords	
		choice=$(zenity --title="$_move_backup_title_" --text="$_move_backup_choose_backup_" \
				--width=850 --height=330 --list --print-column=2\
				--cancel-label="$_move_backup_cancel_" \
				--radiolist --column="" --column="$_backup_name_" --column="$_backup_source_" --column="$_backup_destination_" "${values[@]}")
		if [[ -z "$choice" ]]; then
			break
		fi
		size=${#backups[@]}
		for ((i=0; i<size; i++)); do
			parseRecord "${backups[$i]}"
			if [[ "${values[$NME]}" == "$choice" ]]; then
				newdir=$(zenity --file-selection --directory --title="$_move_backup_choose_new_dir_")
				if [[ -z "$newdir" ]]; then
					echo -e "No new directory => cancel"
					break
				fi
				if [[ ! -w "$newdir" ]]; then
					display_error "$_no_writing_rights_for_dir_"
					break
				fi
				newd="$newdir/$(basename "${values[$DST]}")"
				if [[ ! -d "$newd" ]]; then
					mkdir "$newd"
					if [[ $? -ne 0 ]]; then					
						display_error $(printf "$_move_backup_impossible_to_move_" "$newd")
						break
					fi
				fi
				if [[ -n $(ls "$newd") ]]; then
					display_error $(printf "$_move_backup_dir_not_empty_" "$newd")
					break
				fi
				if [[ ! -d "${values[$DST]}" ]]; then
					display_error $(printf "$_move_backup_dest_not_available_" "${values[$DST]}")
					break
				fi
				rsync -au --progress --delete-after --log-file="$rsync_log" "${values[$DST]}/" "$newd"
				if [[ $? -eq 0 ]]; then
					rm -rf "${values[$DST]}"
					if [[ $? -eq 0 ]]; then	
						zenity --info --text="$_move_backup_successfully_"
					else
						txt=$(printf "$_move_backup_successfully_error_rm_" "${values[$DST]}")
						zenity --info --text="$txt"
					fi
					backups[$i]="${values[$DTE]}|${values[$NME]}|${values[$SRC]}|$newd"
					persist_backups
				else
					txt=$(printf "$_move_backup_impossible_" "$newd")
					display_error "$txt"
				fi
			fi
		done
	done
}
 
remove_backup() {
	load_backups
	choice="-"
	while [[ -n "$choice" ]]; do
		parseAllRecords	
		choice=$(zenity --title="$_rm_backup_title_" --text="$_rm_backup_text_" \
				--width=850 --height=330 --list --print-column=2\
				--cancel-label="$_rm_backup_cancel_button_" \
				--radiolist --column="" --column="$_backup_name_" --column="$_backup_source_" --column="$_backup_destination_" "${values[@]}")
		if [[ -z "$choice" ]]; then
			break
		fi
		size=${#backups[@]}
		for ((i=0; i<size; i++)); do
			parseRecord "${backups[$i]}"
			if [[ "${values[$NME]}" == "$choice" ]]; then
				txt=$(printf "$_rm_backup_really_remove_" "${values[$NME]}")				
				zenity --question --text="$txt"
				if [[ $? -eq 0 ]]; then
					unset backups[$i]
					persist_backups
					rm -rf "${values[$DST]}"
					if [[ $? -eq 0 ]]; then						
						zenity --info --text="$_rm_backup_terminate_successfully_"
					else
						txt=$(printf "$_rm_backup_impossible_" "${values[$DST]})")
						display_error "$txt"
					fi
				fi
			fi
		done
	done
}

edit_default() {
	defdir=$(zenity --file-selection --directory --title="$_default_backup_title_")
	if [[ -n "$defdir" ]]; then 
		if [[ -w "$defdir" ]]; then
			hput "$key_default_dest" "$defdir"
			persist_config
			txt=$(printf "$_default_backup_choosen_" "$defdir")
			zenity --info --width=400 --text="$txt"
		else
			display_error "$_no_writing_rights_for_dir_ ($defdir)"
		fi
	fi
}	

persist_backups() {
	rm -f $dirs_file_tmp 2>/dev/null
	for cs in "${backups[@]}"; do
		if [[ -n "$cs" ]]; then
			echo "$cs" >> $dirs_file_tmp
		fi
	done
	mv $dirs_file_tmp $dirs_file
}

